package product;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import product.CodeList;
import product.SaleList;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class ProductMain5 extends JInternalFrame {

	private JDesktopPane contentPane;

	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@192.168.10.4:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";
	Connection conn;

	String member = "A1";

	CodeInput codeInput = null;
	CodeList codeList = null;
	SaleList saleList = null;
	Sale saleInput = null;

	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenu mnNewMenu_1;
	private JMenu mnNewMenu_2;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;
	private JMenuItem mntmNewMenuItem_4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProductMain5 frame = new ProductMain5();

					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProductMain5() {
		super("", true, true, true, false);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosed(InternalFrameEvent arg0) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 744, 511);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setTitle("코드/판매     사용자 : " + member);
		// 룩엔필

		try {
			UIManager
					.setLookAndFeel("com.jtattoo.plaf.texture.TextureLookAndFeel");

			SwingUtilities.updateComponentTreeUI(this);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		connect();
	}

	public void connect() {
		try {
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url, db_user, db_pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void codeInput() {
		if (codeInput == null) {
			codeInput = new CodeInput(conn, this);
			codeInput.setBounds(20, 20, 931, 525);
			contentPane.add(codeInput);
			codeInput.setVisible(true);
		} else if (codeInput != null) {
			codeInput.setVisible(false);
			codeInput = null;
		}
	}

	public void codeList() {

		if (codeList == null) {
			codeList = new CodeList(conn, this);
			codeList.setBounds(20, 20, 866, 455);
			contentPane.add(codeList);
			codeList.setVisible(true);
		} else if (codeList != null) {
			codeList.setVisible(false);
			codeList = null;
		}

	}

	public void saleInput() {
		if (saleInput == null) {
			saleInput = new Sale(conn, this);
			saleInput.setBounds(20, 20, 772, 434);
			contentPane.add(saleInput);
			saleInput.setVisible(true);
		} else if (saleInput != null) {
			saleInput.setVisible(false);
			saleInput = null;
		}

	}

	public void saleList() {
		if (saleList == null) {
			saleList = new SaleList(conn, this);
			saleList.setBounds(20, 20, 650, 456);
			contentPane.add(saleList);
			saleList.setVisible(true);
		} else if (saleList != null) {
			saleList.setVisible(false);
			saleList = null;
		}
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
			menuBar.add(getMnNewMenu_1());
			menuBar.add(getMnNewMenu_2());
		}
		return menuBar;
	}

	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("임시 사용자");
			mnNewMenu.add(getMntmNewMenuItem());
		}
		return mnNewMenu;
	}

	private JMenu getMnNewMenu_1() {
		if (mnNewMenu_1 == null) {
			mnNewMenu_1 = new JMenu("코 드");
			mnNewMenu_1.add(getMntmNewMenuItem_2());
			mnNewMenu_1.add(getMntmNewMenuItem_1());
		}
		return mnNewMenu_1;
	}

	private JMenu getMnNewMenu_2() {
		if (mnNewMenu_2 == null) {
			mnNewMenu_2 = new JMenu("판 매");
			mnNewMenu_2.add(getMntmNewMenuItem_4());
			mnNewMenu_2.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu_2;
	}

	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("설정");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					member = JOptionPane.showInputDialog("사용자 이름");
					setTitle("코드/판매     사용자 : " + member);
				}
			});
		}
		return mntmNewMenuItem;
	}

	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("코드 목록");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					codeList();
				}
			});
		}
		return mntmNewMenuItem_1;
	}

	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("코드 생성");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					codeInput();
				}
			});
		}
		return mntmNewMenuItem_2;
	}

	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("판매 목록");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					saleList();
				}
			});
		}
		return mntmNewMenuItem_3;
	}

	private JMenuItem getMntmNewMenuItem_4() {
		if (mntmNewMenuItem_4 == null) {
			mntmNewMenuItem_4 = new JMenuItem("판매 생성");
			mntmNewMenuItem_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					saleInput();
				}
			});
		}
		return mntmNewMenuItem_4;
	}
}
